package demo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class LoginCheck{
    private String name;
    private String password;
    public LoginCheck(String name,String password){
        this.name=name;
        this.password=password;
    }
    public boolean equals(){//方法判断用户名密码是否正确，如果正确，返回true
        if("root".equals(name)&&"123456".equals(password)){
            return true;
        }else if("name1".equals(name)&&"pass2".equals(password)) {
        	return true;
        }else{
        	return false;
        }
        
        
    }
};
class ActionHandle{//登录页面及添加需要的按钮
    private static JFrame frame=new JFrame("学生信息管理系统");
    private JTextField name=new JTextField();//设置文本框
    private JPasswordField pass1=new JPasswordField() ;
    private JPasswordField pass=new JPasswordField();
    private JLabel but1=new JLabel("用户名:");
    private JLabel but2=new JLabel("密   码:");
    private JButton but3=new JButton("登录");
    private JLabel but11=new JLabel("确认密码:");
    private JButton but4=new JButton("重置");
    private JButton but5=new JButton("注册");
    private JLabel but7=new JLabel("用户名:");
    private JLabel but8=new JLabel("密   码:");
    private JButton but9=new JButton("确定");
    private JButton but10=new JButton("重置");
    private JLabel but12=new JLabel("性别:");
    ButtonGroup sex=new ButtonGroup(); //设置性别按钮
    JRadioButton male=new JRadioButton("男");
    JRadioButton female=new JRadioButton("女");
    ButtonGroup dxz;
    
    
    public ActionHandle(){//检查用户名和密码是否正确，如正确则进入主页面
        but3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource()==but3){
                    String sname=name.getText();
                    String spass=new String(pass.getPassword());
                    LoginCheck log=new LoginCheck(sname,spass);
                    if(log.equals()){
                        try {
                            new Menu();
                        } catch (Exception e1) {
                            
                            e1.printStackTrace();
                        }
                        frame.setVisible(false);
                        
                    }else{//如密码不正确则报错
                        JOptionPane.showMessageDialog(null, "登录失败，错误的用户名或密码！");
                    }
                }
            }                   
        });  
        
        but4.addActionListener(//重置按钮将用户名和密码归零
new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource()==but4){
                    name.setText("");
                    pass.setText("");
                }
            }
        });
        but5.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		
        		 but9.addActionListener(new ActionListener(){
        	        	public void actionPerformed(ActionEvent e) {
        	        		if(e.getSource()==but9) {
        	        			String name1=name.getText();
        	                    String pass2=new String(pass.getPassword());
        	                    String pass3=new String(pass1.getPassword());
        	                    int a=pass2.length();
        	                    if(pass2.equals(pass3)) {//如果pass1与pass2相同
        	                    	
        	                    }else {//如果两次输入密码不同报错
        	                    	JOptionPane.showMessageDialog(null,"两次密码不同");
        	                    }
        	                    if(a==6 && pass2.equals(pass3)) {//如果密码长度为六并两次输入密码相同则注册成功
        	                    	JOptionPane.showMessageDialog(null,"注册成功");
        	                    	ActionHandle actionHandle = new ActionHandle();
        	                    }else if(a!=6){
        	                    	JOptionPane.showMessageDialog(null,"请输入长度为6的密码");
        	                    }
        	                    if(name1.length()==0) {
        	                    	JOptionPane.showMessageDialog(null,"用户名不能为空");
        	                    }
        	        			
        	        		}
        	        		
        	        		
        	        	}
        	        });
        		
        		JFrame frame1=new JFrame("注册页面");//注册界面及按钮布局
        		frame1.setLocation(100,50);
        		frame1.setSize(400,400);
        		frame1.setVisible(true);
        		frame1.setLayout(null);
        		
        		dxz =new ButtonGroup();//只能选择一个按钮
        	    dxz.add(male);
        	    dxz.add(female);
        		
        		but7.setBounds(80, 30 , 80,30);
                name.setBounds(140,32, 120, 25);    //
                but8.setBounds(80, 70 , 80,30);
                male.setBounds(140, 150 , 80,30);
                female.setBounds(140, 180 , 80,30);
                pass.setBounds(140,72, 120, 25);
                but11.setBounds(80, 110 , 80,30);
                but12.setBounds(80, 150 , 80,30);
                pass1.setBounds(140,112, 120, 25);
                but9.setBounds(90, 230, 80,30);
                but10.setBounds(200,230 , 80,30);
                frame1.add(but7);
                frame1.add(but11);
                frame1.add(but12);
                frame1.add(pass1);
                frame1.add(name);
                frame1.add(pass);
                frame1.add(but8);
                frame1.add(male);
                frame1.add(female);
                frame1.add(but9);
                frame1.add(but10);
                frame1.setVisible(true);
        	    }
        	});
        but10.addActionListener(//重置功能将用户名密码二次密码归零
        		new ActionListener(){
        		            public void actionPerformed(ActionEvent e){
        		                if(e.getSource()==but10){
        		                    name.setText("");
        		                    pass.setText("");
        		                    pass1.setText("");
        		                }
        		            }

        		        });        	
        
       
     
       
        frame.setLayout(null);
        but1.setBounds(80, 40 , 80,30);
        name.setBounds(140,42, 120, 25);    //
        but2.setBounds(80, 80 , 80,30);
        pass.setBounds(140,82, 120, 25);
        but3.setBounds(100, 150 , 60,30);
        but4.setBounds(180, 150 , 60,30);
        but5.setBounds(260,150,60,30);
        frame.setSize(400,330);     
        frame.setLocation(300, 200);
        frame.add(but1);
        frame.add(name);
        frame.add(pass);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but5);
        frame.setVisible(true);
    }
}
public class  Winner{
    public static void main(String[] args) {
        
        new ActionHandle();
       
    }

}

